#include "SwitchAPI.h"
#include "RCSwitch.h"
#include "TCSwitch.h"
#include <string>
#include "esp_timer.h"
#include "esp_log.h"
#include "cJSON.h"
#include "nvs_hashmap.h"

RCSwitch Switch_433 = RCSwitch();
TCSwitch Switch_315 = TCSwitch();

int64_t SwitchTime = 0;
//初始化Switch
void SwitchBegin() {

  Switch_433.enableReceive(27);   // 接收器
  Switch_433.enableTransmit(14);  // 发送器
  Switch_315.enableReceive(25);   // 接收器
  Switch_315.enableTransmit(13);  // 发送器
}


//接收信号，并且保存
void SwitchAvailable() {
    int64_t  Switch;
//   String Base10;
    Switch = esp_timer_get_time();
    char *pattern = (char *)malloc(5);
    unsigned long base10 = 0;
    unsigned int bitLen = 0;
    unsigned int pulse = 0;
    unsigned int protocol = 0;
    memset(pattern,0,5);

    if (Switch > SwitchTime && Switch - SwitchTime > 10) {
        //延迟10毫秒
        SwitchTime = Switch;
        //ESP_LOGI("AV","...%lld",Switch);
        cJSON *cObj = cJSON_CreateObject();
        if (Switch_433.available()) {
            char *jsonSTR = NULL;
            char *key = (char *)malloc(32);
            char *base10_str = (char *)malloc(20);
            base10 = Switch_433.getReceivedValue();
            bitLen = Switch_433.getReceivedBitlength();
            pulse = Switch_433.getReceivedDelay();
            protocol = Switch_433.getReceivedProtocol();
            ESP_LOGI("SwitchAPI","443_rc_value: %lu",base10);

            cJSON_AddStringToObject(cObj,"pattern","F4");
            cJSON_AddNumberToObject(cObj,"base10",base10);
            cJSON_AddNumberToObject(cObj,"bitLen",bitLen);
            cJSON_AddNumberToObject(cObj,"pulse",pulse);
            cJSON_AddNumberToObject(cObj,"protocol",protocol);
            jsonSTR = cJSON_PrintUnformatted(cObj);

            memset(key,0,32);
            memset(base10_str,0,20);
            sprintf(key,"F4%lu",base10);
            snprintf(base10_str,20,"%lu",base10);
            if(strlen(base10_str) > 3){
                ESP_LOGI("SwitchAPI","key: %s => %s",key,jsonSTR);
                nvs_hashmap_put_string(key,jsonSTR);
            }
           
            Switch_433.resetAvailable();
            free(key);
            free(base10_str);
        }

        if(Switch_315.available()){
            char *jsonSTR = NULL;
            char *key = (char *)malloc(32);
            char *base10_str = (char *)malloc(20);
            base10 = Switch_315.getReceivedValue();
            bitLen = Switch_315.getReceivedBitlength();
            pulse = Switch_315.getReceivedDelay();
            protocol = Switch_315.getReceivedProtocol();
            ESP_LOGI("SwitchAPI","315_rc_value: %lu",base10);

            cJSON_AddStringToObject(cObj,"pattern","F3");
            cJSON_AddNumberToObject(cObj,"base10",base10);
            cJSON_AddNumberToObject(cObj,"bitLen",bitLen);
            cJSON_AddNumberToObject(cObj,"pulse",pulse);
            cJSON_AddNumberToObject(cObj,"protocol",protocol);
            jsonSTR = cJSON_PrintUnformatted(cObj);

            memset(key,0,32);
            memset(base10_str,0,20);
            sprintf(key,"F3%lu",base10);
            snprintf(base10_str,20,"%lu",base10);
            if(strlen(base10_str) > 3){
                ESP_LOGI("SwitchAPI","key: %s => %s",key,jsonSTR);
                nvs_hashmap_put_string(key,jsonSTR);
            }
           
            Switch_315.resetAvailable();
            free(key);
            free(base10_str);
        }
        cJSON_Delete(cObj);
    }
    free(pattern);
}

void SwitchSend(char *key){
    int max_len = 256;
    char *jsonSTR = (char *)malloc(max_len);
    memset(jsonSTR,0,max_len);
    esp_err_t err = nvs_hashmap_get_string(key,jsonSTR,max_len);
    if(err == ESP_OK){
        cJSON *cObj = cJSON_Parse(jsonSTR);
        char *pattern = fn_cJSON_GetValueString(cObj,"pattern");
        unsigned long base10 = fn_cJSON_GetValueInt(cObj,"base10");
        unsigned int bitLen = fn_cJSON_GetValueInt(cObj,"bitLen");
        unsigned int pulse = fn_cJSON_GetValueInt(cObj,"pulse");
        unsigned int protocol = fn_cJSON_GetValueInt(cObj,"protocol");
        ESP_LOGI("SwitchAPI","%s: %s",pattern,jsonSTR);
        if(strcmp("F4",pattern) == 0){
            ESP_LOGI("SwitchAPI","base10:%d, bitLen: %d, pulse: %d, protocol: %d",base10,bitLen,pulse,protocol);
            Switch_433.setProtocol(protocol);
            Switch_433.setPulseLength(pulse);
            Switch_433.send(base10, bitLen);
            
        }

        if(strcmp("F3",pattern) == 0){
            ESP_LOGI("SwitchAPI","base10:%d, bitLen: %d, pulse: %d, protocol: %d",base10,bitLen,pulse,protocol);
            Switch_315.setProtocol(protocol);
            Switch_315.setPulseLength(pulse);
            Switch_315.send(base10, bitLen);
            
        }
        cJSON_Delete(cObj);
    }

    free(jsonSTR);
}